package br.com.gsc.controller;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import br.com.gsc.model.tableMapping.Person;
import br.com.gsc.model.tableMapping.Post;
import br.com.gsc.model.tableMapping.Product;
import br.com.gsc.model.tableMapping.Topic;
import br.com.gsc.repository.objRepos.PersonRepository;
import br.com.gsc.repository.objRepos.PostRepository;
import br.com.gsc.repository.objRepos.ProductRepository;
import br.com.gsc.repository.objRepos.TopicRepository;

@Controller
@RequestMapping("/topico/**")
public class TopicController {

	@Autowired
	private PostRepository postRepo;
	@Autowired
	private TopicRepository top;
	@Autowired
	private PersonRepository per;
	@Autowired
	private ProductRepository pro;
	@Autowired
	private TopicControllerHelper topHelper;
	@Autowired
	@Qualifier("authentication")
	private Authentication auth;
	

	
	@RequestMapping(value="/topico/{topicId}", method=RequestMethod.GET)
	public ModelAndView showPage(@PathVariable("topicId") int id){
		ModelAndView model = new ModelAndView("topico");
		Topic topic = top.findTopicByID((long) id);
		model.addObject("topic",topic);	
		Post post = new Post();
		post.setPerson(topic.getPerson());
		post.setTopic(topic);
		model.addObject("post",post);
		model.addObject("logged",auth.getName());
		return model;
	}
	
	@RequestMapping(value="/topico/{topicId}", method=RequestMethod.PUT)
	public String editTopic(@PathVariable("topicId") int id){
		Topic topic = top.findTopicByID((long) id);
		//TODO
		return "redirect:/topico/"+id+".html";
	}
	
	@RequestMapping(value="/topico/{topicId}", method=RequestMethod.DELETE)
	public String deleteTopic(@PathVariable("topicId") int id){
		Topic topic = top.findTopicByID((long) id);
		top.removeTopic(topic);
		return "redirect:/";
	}
	
	
	@RequestMapping(method = RequestMethod.POST, value="/topico/addtopic")
	public ModelAndView addtopic(@ModelAttribute("topic") @Valid Topic topic, BindingResult result){		
		
		if(result.hasErrors()){			
			return topHelper.initInternPage();
		}			
		
		if(auth.isAuthenticated()){
			Topic newTopic = new Topic();
			newTopic.setTopicTitle(topic.getTopicTitle());
			newTopic.setTopicContent(topic.getTopicContent());
			newTopic.setTopicType(topic.getTopicType());
			newTopic.setCreatedIn(new Date());
			Person p = per.findPersonByID(auth.getName());
			newTopic.setPerson(p);
			Product pro = new Product();
			pro.setName("teste");
			pro.setId(1);
			newTopic.setProduct(pro);
			top.addTopic(newTopic);
			return new ModelAndView("redirect:/");
		}else{
			return new ModelAndView("forward:/user/index.html");
		}
	}
	
}
